//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "server\fn_serverSetupTakeOverExec.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_input","_params","_id","_result"];
_input = _this;
_result = false;
if (IS_ARRAY(_input) && {count _input > 2}) then {
	_id = GETVAL(_input,0,"");
	_params = GETVAL(_input,2,[]);
	TRACE_1("Extracted",ARR_2(_id,_params));
	if (IS_ARRAY(_params) && {count _params > 0}) then {
		private "_object";
		_object = GETVAL(_params,0,objNull);
		if (!isNull _object) then {
			private ["_instance_id","_entities","_taken","_hold_count","_count_owners","_count_takers","_count_soldiers","_action","_parameters","_zone_size","_takenover_msg","_lost_msg","_shared_takeover","_hold_time"
					,"_action_id","_owner_side","_taker_side","_takenover_code","_lost_code","_share_dist","_taken_time","_last_run"];
			_action_id  = GETVAL(_params,1,0);
			_instance_id  = GETVAL(_params,2,0);
			_zone_size = GETVAL(_params,3,50);
			_hold_time = GETVAL(_params,4,15);
			_owner_side = GETVAL(_params,5,[]);
			_taker_side = GETVAL(_params,6,[]);
			_share_dist = GETVAL(_params,7,300);
			
			_last_run = _object getVariable [format["%1_%2",QGVAR(Last_Run),_action_id],time];
			_hold_count = _object getVariable [format["%1_%2",QGVAR(Hold_Count),_action_id],time];
			_taken_time = _object getVariable [format["%1_%2",QGVAR(Taken_Time),_action_id],time];
			_taken = _object getVariable [format["%1_%2",QGVAR(Taken),_action_id],false];
			
			_entities = (getPos _object) nearEntities [["Man","LandVehicle"], _zone_size];
			
			_count_owners = 0;
			{ _count_owners = _count_owners + (_x countSide _entities);
			} foreach _owner_side;
			_count_takers = 0;
			{ _count_takers = _count_takers + (_x countSide _entities);
			} foreach _taker_side;
			TRACE_1("Counts",ARR_7(_object,getPos(_object),_zone_size,_id,_hold_count,_count_owners,_count_takers));

			// If taker inside we take the bunker
			if ((_count_owners <= 0) and (_count_takers > 0)) then {
				_hold_count = _hold_count + (time - _last_run);
			};
			// If owner inside, we reown the bunker, two times faster (its the owner after all)
			if ((_count_owners > 0) and (_count_takers <= 0)) then {
				_hold_count = _hold_count - (2 * (time - _last_run));
			};
			// If nobody inside, we set the variable back to the current status
			if ((_count_owners == 0) and (_count_takers == 0)) then {
				if (_taken) then { _hold_count = _hold_time }
				else { _hold_count = 0 };
			};
			if (_hold_count <= 0) then {_hold_count = 0};
			if (_hold_count >= _hold_time) then {_hold_count = _hold_time};
			if ((_hold_count >= _hold_time) && !_taken) then {
				_taken = true;
				TRACE_1("Taken",ARR_4(_id,_hold_count,_count_owners,_count_takers));
				
				_action = [_object,_action_id] call FFUNC(actionGet);
				_parameters = [_action] call FFUNC(actionGetParams);
				
				_takenover_msg = [_parameters, "TAKEN_MSG", "",THIS_FILE] call FFUNC(hashGet);
				_shared_takeover = [_parameters, "SHARED_VAR", "",THIS_FILE] call FFUNC(hashGet);
				_takenover_code = [_parameters, "TAKEN_CODE", "",THIS_FILE] call FFUNC(hashGet);
				_shared_takeover = _shared_takeover call FFUNC(getStringVar);
				
				if (!isNil "_takenover_msg") then {
					if (count _takenover_msg > 0) then {[QGVAR(all_message),[_takenover_msg,"all",objNull,"hq"],THIS_FILE] call FFUNC(callEvent);};
				};
				if (!isNil "_shared_takeover") then {
					if (_shared_takeover != "") then {
						[_instance_id,_shared_takeover,_taken] spawn FFUNC(setMissionJIPVar);
						[QGVAR(all_var), [_instance_id,_shared_takeover,_taken],THIS_FILE] call FFUNC(callEvent);
					};
				};
				if (_takenover_code != "") then {
					[QGVAR(all_execute),[_takenover_code,_object,_action],THIS_FILE] call FFUNC(callEvent);
				};			
				_taken_time = time;

				[[(getPos _object),(_zone_size / 2)], _owner_side, _share_dist,["Man","Car","Motorcycle"]] spawn FFUNC(convergeUnits);
			};
			if ((_hold_count <= 0) && _taken) then {
				_taken = false;
				TRACE_1("Lost",ARR_4(_id,_hold_count,_count_owners,_count_takers));
				
				_action = [_object,_action_id] call FFUNC(actionGet);
				_parameters = [_action] call FFUNC(actionGetParams);
				
				_lost_msg = [_parameters, "LOST_MSG", "",THIS_FILE] call FFUNC(hashGet);
				_shared_takeover = [_parameters, "SHARED_VAR", "",THIS_FILE] call FFUNC(hashGet);
				_lost_code = [_parameters, "LOST_CODE", "",THIS_FILE] call FFUNC(hashGet);
				_shared_takeover = _shared_takeover call FFUNC(getStringVar);

				if (!isNil "_lost_msg") then {
					if (count _lost_msg > 0) then {[QGVAR(all_message),[_lost_msg,"all",objNull,"hq"],THIS_FILE] call FFUNC(callEvent);};
				};
				if (!isNil "_shared_takeover") then {
					if (_shared_takeover != "") then {
						[_instance_id,_shared_takeover,_taken] spawn FFUNC(setMissionJIPVar);
						[QGVAR(all_var), [_instance_id,_shared_takeover,_taken],THIS_FILE] call FFUNC(callEvent);
					};
				};
				if (_lost_code != "") then {
					[QGVAR(all_execute),[_lost_code,_object,_action],THIS_FILE] call FFUNC(callEvent);
				};			
			};
			// If still taken after 5 minutes, we call more groups.
			if (_taken && ((time - _taken_time) > 300)) then {
				// Continue to call units if still not retaken but only men who can actually enter
				[[(getPos _object),(_zone_size / 2)], _owner_side, _share_dist,["Man"]] spawn FFUNC(convergeUnits);
				_taken_time = time;
			};	
			_object setVariable [format["%1_%2",QGVAR(Hold_Count),_action_id],_hold_count];
			_object setVariable [format["%1_%2",QGVAR(Taken),_action_id],_taken];
			_object setVariable [format["%1_%2",QGVAR(Taken_Time),_action_id],_taken_time];
			_object setVariable [format["%1_%2",QGVAR(Last_Run),_action_id],time];
			
			if (_object getVariable [format["%1_%2",QGVAR(End_Check),_action_id],true]) then {
				["itemRemove", _id] call mso_fnc_fsmloop;
				DEBUG_1("Item removed",_id);
			};
		} else {
			DEBUG_1("Object is null",_params);
		};
	} else {
		DEBUG_1("Invalid params",_params);
	};
} else {
	DEBUG_1("Invalid input",_input);
};
TRACE("End");
TMFPROFILERSTOP;